package com.zqxq.lottery.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.entity.vo.user.UserAddressVO;
import com.zqxq.lottery.entity.vo.user.UserProductTradDetailVO;
import com.zqxq.lottery.entity.vo.user.UserProductTradExcelVO;
import com.zqxq.lottery.entity.vo.user.UserProductTradVO;
import com.zqxq.lottery.service.user.IUserProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 苦智
 * @date 2018/9/10 11:07
 * @description 用户产品交易
 * @ClassName UserProductController
 */
@RequestMapping("/trade")
@Slf4j
@RestController
public class UserProductController {
    @Autowired
    private IUserProductService userProductService;
    @PostMapping("/page/list")
    public Result getUserProductPage(@RequestBody Map<String,Object> paramMap){
        log.info("请求参数={}", JSONObject.toJSONString(paramMap));
        Page page = new Page(Integer.parseInt(paramMap.get("pageNO").toString()), Integer.parseInt(paramMap.get("pageSize").toString()));
        List<UserProductTradVO> list = userProductService.getUserProductPage(page,paramMap);
        page.setRecords(list);
        BigDecimal totalPrice = userProductService.getUserProductTotalPrice(paramMap);
        Map<String,Object> map = new HashMap<>();
        map.put("totalPrice",totalPrice);
        page.setCondition(map);
        return new Result(CodeEnum.SUCCESS.getCode(),"成功",page);
    }

    @PostMapping("/list")
    public Result<List<UserProductTradExcelVO>> getUserProductList(@RequestBody Map<String,Object> paramMap){
        List<UserProductTradExcelVO> list = userProductService.getUserProductList(paramMap);
        if (list == null && list.size() < 1){
            return new Result(CodeEnum.FAIL.getCode(),CodeEnum.FAIL.getMsg());
        }
        return new Result<List<UserProductTradExcelVO>>(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg(),list);
    }

    @PostMapping("/detail")
    public Result getUserProductDetail(String tradeId){
        if (StrUtils.isEmpty(tradeId))
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        UserProductTradDetailVO vo = userProductService.getUserProductDetail(tradeId);
        return new Result(CodeEnum.SUCCESS.getCode(),"成功",vo);
    }

    @PostMapping("/delivery")
    public Result deliveryToUser(String tradeId, String logisticsNum, String logisticsCompany){
        if (StrUtils.isEmpty(tradeId) || StrUtils.isEmpty(logisticsNum) || StrUtils.isEmpty(logisticsCompany)){
            return new Result(CodeEnum.FAIL_100002.getCode(),"交易id不能为空");
        }
        int status = userProductService.updateUserProductStatus(tradeId,logisticsNum,logisticsCompany);
        if (status == 1)
            return new Result(CodeEnum.SUCCESS.getCode(),"发货成功");
        return new Result(CodeEnum.FAIL.getCode(),"发货失败，请检查后重试");
    }

    @PostMapping("/edit")
    public Result updateReceivingInfo(@RequestBody UserAddressVO userAddressVO){
        if (userAddressVO == null || StrUtils.isEmpty(userAddressVO.getProvince()) ||
                StrUtils.isEmpty(userAddressVO.getCity()) || StrUtils.isEmpty(userAddressVO.getCounty()) ||
                StrUtils.isEmpty(userAddressVO.getStreet()) || StrUtils.isEmpty(userAddressVO.getDetailAddress()) ||
                StrUtils.isEmpty(userAddressVO.getUserId()))
            return new Result(CodeEnum.FAIL_100002.getCode(),"地址参数不能为空");
        int status = userProductService.updateReceivingInfo(userAddressVO);
        if (status == 1)
            return new Result(CodeEnum.SUCCESS.getCode(),"成功");
        return new Result(CodeEnum.FAIL.getCode(),"地址编辑失败");
    }

    @PostMapping("/page")
    public Result page(@RequestParam("userId") Long userId,Integer status,@RequestParam("pageNo") Integer pageNo,@RequestParam("pageSize") Integer pageSize){
        Result rs = new Result();
        try{
            Page page = userProductService.queryPage(userId,status,pageNo,pageSize);
            rs.setData(page);
        }catch(Exception e){
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("UserProduct Page  Error:",e);
        }
        return rs;
    }

    @PostMapping("/get")
    public Result getUserProduct(String tradeId){
        if (StrUtils.isEmpty(tradeId))
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        UserProductTradDetailVO vo = userProductService.getUserProduct(tradeId);
        return new Result(CodeEnum.SUCCESS.getCode(),"成功",vo);
    }

    @PostMapping("/cancelUserProduct")
    public Result cancelUserProduct(String id){
        Result result = new Result();
        if (StrUtils.isEmpty(id)) {
            return new Result(CodeEnum.FAIL_100002.getCode(), CodeEnum.FAIL_100002.getMsg());
        }
        result = userProductService.cancelUserProduct(id);
        return result;
    }

}
